package m.html.util;

import m.html.element.Button;
import m.html.element.Element;
import m.html.element.Text;
import m.html.element.attr.ButtonType;
import m.html.element.other.DisplayItem;
import m.html.element.other.IElement;
import m.html.list.ListSearch;
import m.system.util.ArrayUtil;
import m.system.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class VueListUtil {

    public static <T extends VueListColumn> Element toViewColumnElement(List<T> cols, Map<String,String> params){
        Element listElement=Element.tag("a-list").A("size","small").A("style","max-height:300px;overflow-y:auto;");
        for(VueListColumn col : cols) {
            listElement.E(
                Element.tag("a-list-item")
                    .E(
                        Element.tag("a-list-item-meta")
                            .E(
                                Element.tag("template").A("#avatar")
                                    .E(Element.tag("a-switch").A("v-model:checked","inputParams['view_"+col.getFieldAs()+"']").A("size","small").A("checkedValue","true").A("unCheckedValue","false"))
                            ),
                        Element.tag("div").A("style", "flex:3")
                            .E(new Text(col.getTitle()+" "+ ArrayUtil.connection(col.getGroupTitle(), ",")))
                    )
            );
            params.put("view_"+col.getFieldAs(), String.valueOf(col.getShow()));
        }
        Element viewColElement=Element.tag("div").A("style", "width:200px;margin:-12px -16px 0;");
        viewColElement.E(listElement);
        viewColElement.E(
            Element.tag("div").A("style","padding-top:8px;text-align:center;border-top: 1px solid #8884;")
                .E(new Button(ButtonType.PRIMARY).A("size","small").A("style", "width:130px;").E(new Text("确定")).setExtend(" @click=\"queryData(1);\" "))
        );
        return viewColElement;
    }

    public static Element toSearchPanel(List<ListSearch> searchMap){
        boolean hasSearch=false;
        List<IElement> searchItems= new ArrayList<>();
        for(ListSearch ls : searchMap) {
            String tx=ls.toString();
            if(!StringUtil.isSpace(tx)) {
                hasSearch=true;
                searchItems.add(
                    Element.tag("a-form-item").A("label",ls.getTitle())
                        .E(new Text(tx))
                );
            }
        }
        Element e=Element.tag("a-drawer")
        .A("width","580px").A("v-model:visible","showSearch").A(":closable","false").A("wrapClassName","index-drawer")
        .E(Element.tag("a-page-header").A("@back","showSearch=false;").A("title","筛选"))
        .E(
            Element.tag("a-card").A("hoverable").A("style","margin:0 20px;")
                .E(
                    Element.tag("a-form").A(":label-col","{span:6}").A(":wrapper-col","{span:18}")
                        .E(searchItems)
                )

        )
        .E(
            Element.tag("div").A("style","padding:10px 20px 20px;text-align:right;")
                .E(new Button(ButtonType.PRIMARY).E(new Text("查询")).setExtend(" @click=\"queryData(1);\" "))
        );
        return hasSearch?e:null;
    }
    public static Element toText(String dateFormat,String numberFormat,String dictType,List<DisplayItem> disItems,boolean copy){
        if(!StringUtil.isSpace(dateFormat)) {//含日期格式
            if(copy)
                return Element.tag("a-typography-paragraph")
                    .A(":copyable","text?{text:formatter('date',text,'"+dateFormat+"')}:false")
                    .E(new Text("{{formatter('date',text,'"+dateFormat+"')}}"));
            else
                return new Text("{{formatter('date',text,'"+dateFormat+"')}}");
        }else if(!StringUtil.isSpace(numberFormat)) {//含数字格式
            if(copy)
                return Element.tag("a-typography-paragraph")
                    .A(":copyable","text?{text:formatter('number',text,'"+numberFormat+"')}:false")
                    .E(new Text("{{formatter('number',text,'"+numberFormat+"')}}"));
            else
                return new Text("{{formatter('number',text,'"+numberFormat+"')}}");
        }else if(!StringUtil.isSpace(dictType)){//有字典项
            if(copy)
                return Element.tag("a-typography-paragraph")
                    .A(":copyable","text?{text:dict('"+dictType+"',text)}:false")
                    .E(new Text("{{dict('"+dictType+"',text)}}"));
            else
                return Element.tag("span").A(":title","dict('"+dictType+"',text)").E(new Text("{{dict('"+dictType+"',text)}}"));
        }else if(disItems.size()>0){//有显示选项
            Element tmp=Element.tag("span");
            for (DisplayItem item : disItems) {
                tmp.E(Element.tag("span").A("v-if", "outTxt(text)=='" + item.getValue() + "'").E(new Text(item.getTitle())));
            }
            return tmp;
        }else {
            if(copy)
                return Element.tag("a-typography-paragraph")
                    .A(":copyable","text?{text:text}:false")
                    .E(Element.tag("span").A(":title","text").E(new Text("{{outTxt(text)}}")));
            else
                return Element.tag("span").A(":title","text").E(new Text("{{outTxt(text)}}"));
        }
    }

}
